// pages/modifyvoucher/modifyvoucher.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '修改凭证', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    loadModal: true,
    height: app.globalData.height,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    _this.setData({ shop_id: options.shop_id })
    $api.getApplyShopDetail({
      login_token: wx.getStorageSync('login_token'),
      has_order: 1,
      shop_id: options.shop_id,
      is_detail: 0,
    }).then((res) => {
      let imglist = res.data.data.shop_info.fukuan_imgs ? JSON.parse(res.data.data.shop_info.fukuan_imgs) : []
      let order_id = res.data.data.shop_info.order_id
      _this.setData({ imglist, order_id })
    })
  },
  chooseImage: function () {
    let _this = this
    let imglist = _this.data.imglist
    $api.uploadImagesdt(true).then((res) => {
      let info = res
      info.forEach((item) => {
        imglist.push(item.url)
      })
      _this.setData({ imglist })
      wx.setStorageSync('imglist', imglist)
    })

  },
  onCloseImgs(e) {
    let imglist = this.data.imglist
    imglist.splice(e.currentTarget.dataset.index, 1)
    console.log(imglist)
    this.setData({ imglist })
  },
  sum() {
    let _this = this
    let imglist = _this.data.imglist
    if (imglist.length == 0) {
      wx.showToast({
        title: '请上传付款凭证',
        icon: 'none',
        duration: 2000
      })
      return
    }
    $api.editorderfukimgs({
      login_token: wx.getStorageSync('login_token'),
      has_order: 1,
      fukuan_imgs: JSON.stringify(imglist),
      order_id: _this.data.order_id,
    }).then((res) => {
      if (res.data.code == 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        wx.navigateBack({
          delta: 1,
          fail: (res) => {
            console.log(res)
            wx.reLaunch({
              url: '/pages/home/home',
            })
          },
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
})